﻿using BaseUtils.Helper;
using BaseUtils.Logger;
using BaseUtils.Utils;
using NW_WEB_Service.com.app.entities;
using NW_WEB_Service.com.app.entities.modal;
using NWMES.Mes;
using NWMES.Model;
using Service.src.com.utils.modal;
using Service.src.Flow.Server;
using Service.src.Model;
using Service.src.Model.Folw;
using Service.src.NW.Model;
using System;
using System.Web.Http;

namespace NW_WEB_Service.com.app.controllers
{
    [RoutePrefix("api/node")]
    public class NodeController : ApiController
    {

        [HttpGet]
        [Route("list/hub")]
        public HttpResult GetNodeList()
        {
            HttpResult Result = new HttpResult();
            Groupre sourceNodeGroups = FileHelper.ReadContent<Groupre>($"{AppData.AppPath}{AppData.NodeHubName}");
            //return sourceNodeGroups.ToJson();

            return Result.Success(sourceNodeGroups);
        }

        [HttpGet]
        [Route("{Id}")]
        public HttpResult GetNode(int Id)
        {
            NodeProcessor Node = null;
            foreach (var Root in AppData.OpenFlows)
            {
                if (Node != null)
                    break;
                foreach (NodeProcessor Item in Root.Value.processors)
                {
                    if (Item.id == Id)
                    {
                        Node = Item;
                        break;
                    }
                }
            }

            HttpResult Result = new HttpResult();
            return Result.Success(Node);
        }
        /// <summary>
        /// 更新节点的运行参数
        /// </summary>
        /// <param name="FolwId"></param>
        /// <param name="NodeItemDataJson"></param>
        /// <param name="GroupJson"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("update-node-param")]
        public HttpResult UpdateNodeParam([FromBody] UpdateNodeParamDTO UpdateNodeParam)
        {

            FlowServer flowServer = new FlowServer();
            try
            {
                FlowRoot Root = flowServer.UpdateFlowNodeParams(UpdateNodeParam);
            }
            catch (Exception ex)
            {
                LoggerService.Error($"更新节点参数异常，原因：" + ex.Message, ex);
                return new HttpResult().Error($"更新节点参数异常，原因：" + ex.Message);
            }
            // string t = "D:\\project\\number-smart\\007-NW-AUTO-FLOW\\nw-auto-flow\\NW-AUTO-FLOW\\edit\\Service\\bin\\Debug\\projects\\flow\\NEW_2\\index.nw";
            // UpdateResult updateResult = JsonHelper.WriteValue(t, "$.namet", "1sd 23大富大贵");
            return new HttpResult().Success(UpdateNodeParam);
        }

        /// <summary>
        /// 删除节点
        /// 删除连线
        /// </summary>
        /// <param name="FolwId"></param>
        /// <param name="NodeItemJson"></param>
        /// <param name="GroupJson"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route]
        public HttpResult DeleteFlowNode([FromUri] long FlowId, string NodeIds = null, string LinkIds = null)
        {
            FlowServer flowServer = new FlowServer();
            string Res = flowServer.DeleteFlowNode(FlowId, NodeIds, LinkIds);
            return new HttpResult().Success(Res);
        }
        /// <summary>
        /// 添加流程节点
        /// </summary>
        /// <param name="FolwId"></param>
        /// <param name="TypeId"></param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="MaxX"></param>
        /// <param name="MaxY"></param>
        /// <param name="GroupId"></param>
        /// <returns></returns>
        [HttpPost]
        [Route]
        public HttpResult Save([FromBody] NodeVO Node)
        {
            FlowServer flowServer = new FlowServer();
            string Flow = flowServer.AddNode(Node);
            return new HttpResult().Success(Flow);
        }

        /// <summary>
        /// 创建节点连线
        /// </summary>
        /// <param name="FolwId"></param>
        /// <param name="SourceNodeId"></param>
        /// <param name="TargetNodeId"></param>
        /// <param name="SourcePort"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("connection")]
        public HttpResult CreateConnection([FromBody] ConnectionDTO Connection)
        {
            FlowServer flowServer = new FlowServer();
            string CC = flowServer.CreateConnection(Connection);
            return new HttpResult().Success(CC);
        }

        /// <summary>
        /// 更新mes的请求参数
        /// </summary>
        /// <param name="ApiName"></param>
        /// <param name="RequestJson"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("save-node-mes-config")]
        public HttpResult SaveMesNodeRequestParams([FromBody] NodeRequestParamDTO NodeRequestParam)
        {
            MesBaseEntity mesBaseEntity = ConvertHelper.ToObject<MesBaseEntity>(NodeRequestParam.RequestJson);
            new MESBaseService().UpdateMesRequestParams(NodeRequestParam.ApiName, mesBaseEntity);
            return new HttpResult().Success();
        }

        [HttpGet]
        [Route("node-config")]
        public HttpResult GetMesNodeRequestParams(string ApiName)
        {
            MesBaseEntity Config = null;
            try
            {
                Config = MesBaseEntity.config[ApiName].ToObject<MesBaseEntity>();
            }
            catch (System.Exception ex)
            {
                LoggerService.Error("请检查MES配置文件是否存在", ex);
            }
            return new HttpResult().Success(Config);
        }
    }
}
